package com.htfyun.roobofactory.mediaUtils.myrecorder;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class RecorderOperator {
    private static AudioRecord mRecord;
    // 音频获取源
    private int audioSource = MediaRecorder.AudioSource.MIC;

    // 设置音频采样率，44100是目前的标准，但是某些设备仍然支持22050，16000，11025
    private static int sampleRateInHz = 16000;// 44100;
    // 设置音频的录制的声道CHANNEL_IN_STEREO为双声道，CHANNEL_CONFIGURATION_MONO为单声道
    private static int channelConfig = AudioFormat.CHANNEL_IN_STEREO;// AudioFormat.CHANNEL_IN_STEREO;
    // 音频数据格式:PCM 16位每个样本。保证设备支持。PCM 8位每个样本。不一定能得到设备支持。
    private static int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
    // 音频大小
    private int minBufSize;
    private String recordPath ="";
    private IRecordListener mIRecordListener;
    private boolean isRecording = false;
    private static RecorderOperator instance;
    public static RecorderOperator getInstance(){
        if(null == instance){
            instance = new RecorderOperator();
        }
        return instance;
    }

    public RecorderOperator(){
        minBufSize = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig,
                audioFormat);
        mRecord = new AudioRecord(audioSource, sampleRateInHz, channelConfig,
                audioFormat, minBufSize);
    }

    public void setRecordPath(String path){
        if(path!=null && path.length()>0){
            recordPath = path;
        }
    }


    public void setRecordingListener(IRecordListener listener){
        mIRecordListener = listener;
    }
    public void startRecord(){
        isRecording = true;
        //开启一个线程录音
        byte[] audiodata = new byte[minBufSize];
        mRecord.startRecording();
        if(mIRecordListener!=null){
            mIRecordListener.onRecordingStart();
        }
        int readsize = 0;
        FileOutputStream fos = null;
        if(recordPath!=null && recordPath.length()>0){
            try {
                File file = new File(recordPath);
                if (file.exists()) {
                    Log.i("删除原有文件", String.valueOf(readsize));
                    file.delete();
                }else{
                    file.createNewFile();
                    Log.i("create原有文件", String.valueOf(readsize));
                }
                fos = new FileOutputStream(file);// 建立一个可存取字节的文件
            } catch (Exception e) {
                e.printStackTrace();
                fos = null;
            }
        }

        while (isRecording == true) {
            readsize = mRecord.read(audiodata, 0, minBufSize);
            if(mIRecordListener!=null){
                mIRecordListener.onRecordingData(audiodata);
            }
            Log.i("采集大小", String.valueOf(readsize));
            if (AudioRecord.ERROR_INVALID_OPERATION != readsize) {
                if(fos!=null){
                    try {
                        fos.write(audiodata);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        mRecord.stop();
        if(mIRecordListener!=null){
            mIRecordListener.onRecordingStop();
        }
        if(fos!=null){
            try {
                fos.close();// 关闭写入流
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void stopRecord(){
        isRecording = false;

    }
}

